<?php
/**
 * mvc核心类库 myf.php
 * User: www.minyifei.cn
 * Date: 15/4/23
 */

require_once("core.php");
//请求唯一编号
$uuid = md5(uniqid(mt_rand(), true));
session("myfapi_uuid",$uuid);
//获取路由解析器
$route = getMvcRoute();
//上行参数日志
\Myf\Mvc\Log::write(sprintf("uuid=%s,url=【%s】,params=【%s】",$uuid,session("_url"),JSON($_REQUEST)));
//控制器
$mvcController = $route["c"];
//执行方法
$mvcAction = $route["a"];
//控制器的命名规则为：XxxController
$mvcControllerName = $mvcController."Controller";
//加载控制器文件
$mvcControllerFile = sprintf("%s/app/controller/%s.php",APP_PATH,$mvcControllerName);
if(file_exists($mvcControllerFile)){
    //加载基类controller
    require_once APP_SYS_PATH."/controller.php";
    require_once $mvcControllerFile;
    $myfC = new $mvcControllerName();
    try{
        //初始化系统方法
        $myfC->_sys_init_action($mvcController);
        //执行前置方法
        $myfC->_before_action();
        //执行用户方法
        $myfC->{$mvcAction}();
        //执行后置方法
        $myfC->_after_action();
    }catch (Exception $e){
        $myfC->echoError($e->getMessage(),$e->getCode());
    }
}else{
    $error = sprintf("访问的接口不存在,url=%s",session("_url"));
    $res = array(
        "uuid"=>$uuid,
        "code"=>\MyfApi\Libs\ErrorCode::FAIL,
        "error"=>$error,
    );
    \Myf\Mvc\Log::write(sprintf("uuid=%s,status=【%s】,response=【%s】",$uuid,"fail",JSON($res)));
    echoJson($res);
}
